package cn.gqr.dao;

import cn.gqr.entity.*;
import cn.gqr.uitl.SessionUitl;
import com.mongodb.client.result.DeleteResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import java.awt.print.Book;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

@Repository
public class BolgsDao {

    @Autowired
    MongoTemplate mT;

    @Autowired
    TypeDao td;

    @Autowired
    UserDao ud;


    //根据id查找博客
    public Blog findById(String id){
        Criteria c = new Criteria();
        return mT.findOne(new Query(c.where("_id").is(id)), Blog.class);
    }

    //添加博客
    public Blog addBlog(Blog blog){
       return mT.insert(blog);
    }

    //删除博客
    public boolean delById(String id){
        Criteria c = new Criteria();
        return mT.remove(new Query(c.where("id").is(id)),Blog.class).wasAcknowledged();
    }

    //分页查询博客
    //参数 start 当前页数  title 博客标题  title == null 分页全查询   title != null 根据标题分页模糊查询
    public List<Blog> findByTitleOrTypePage(int start,String title,String typeId){

        Pageable of = PageRequest.of(start<0?0:start, 3);
        Query q = new Query().with(of);

        if(title!=null){
            Pattern pattern = Pattern.compile("^.*" + title + ".*$", Pattern.CASE_INSENSITIVE);//模糊匹配
            Criteria c = new Criteria().and("title").regex(pattern);
            q.addCriteria(c);
        }

        if(typeId!=null){
            Criteria c = Criteria.where("type_id").is(typeId);
            q.addCriteria(c);
        }

        return mT.find(q, Blog.class);
    }


    //查询分页查询后的 博客/类型 数据行数
    public long findByTitlePageCount(String title,String typeId,Class cla){

        Query q = new Query();

        if(title!=null){
            Pattern pattern = Pattern.compile("^.*" + title + ".*$", Pattern.CASE_INSENSITIVE);//模糊匹配
            Criteria c = new Criteria().and("title").regex(pattern);
            q.addCriteria(c);
            return mT.count(q,Blog.class);
        }

        if(typeId!=null){
            Criteria c = Criteria.where("type_id").is(typeId);
            q.addCriteria(c);
            return mT.count(q,Blog.class);
        }

        return mT.count(q,cla);
    }


    //根据博客id添加阅读量
    public boolean addReadCount(Blog blog){
        Criteria c = new Criteria();
        Query q = new Query(c.where("id").is(blog.getId()));
        Update up = new Update().set("read_count",blog.getReadCount()+1);
        return mT.updateFirst(q,up,Blog.class).wasAcknowledged();
    }

    //根据登录后的用户id查找博客
    public List<Blog> findByUidPage(int start){
        Pageable of = PageRequest.of(start<0?0:start, 3);
        Query q = new Query().with(of);
        q.addCriteria(Criteria.where("uid").is(SessionUitl.user().getId()));
        List<Blog> blogs = mT.find(q, Blog.class);
        return blogs;
    }

    //根据登录后的用户id查找博客
    public long findByUidPageCount(){
        Query q = new Query();
        q.addCriteria(Criteria.where("uid").is(SessionUitl.user().getId()));
        return mT.count(q,Blog.class);
    }

}
